﻿namespace BlackHole.Blackhole
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using BlackHole.DAO;

    /// <summary>
    /// 产品维护页面
    /// </summary>
    public partial class Maintance : System.Web.UI.Page
    {
        /// <summary>
        /// 初始化控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.RenderUI();
            }
        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        protected void RenderUI()
        {
            this.RefreshDeviceList();
            this.RefreshDistrictList();
            this.RefreshOwnerList(0);
        }

        /// <summary>
        /// 事件更改区县
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LstDistricts_SelectedIndexChanged(object sender, EventArgs e)
        {
            int ownerId;
            ownerId = int.Parse(this.lstDistricts.SelectedValue);
            if (ownerId != 0)
            {
                this.RefreshOwnerList(ownerId);
            }

            this.ShowAddButton(2);
        }

        /// <summary>
        /// 刷新设备列表
        /// </summary>
        protected void RefreshDeviceList()
        {
            this.txtAddName.Text = string.Empty;

            this.lstDevices.Items.Clear();
            this.lstDevices.Items.Add(new ListItem("(此处新增)", "0"));
            foreach (Device d in Utility.GetAllDevices())
            {
                this.lstDevices.Items.Add(new ListItem(d.DeviceName, d.Id.ToString()));
            }
        }

        /// <summary>
        /// 刷新区县列表
        /// </summary>
        protected void RefreshDistrictList()
        {
            this.lstDistricts.Items.Clear();
            this.lstDistricts.Items.Add(new ListItem("(此处新增)", "0"));
            foreach (District d in Utility.GetAllDistricts())
            {
                this.lstDistricts.Items.Add(new ListItem(d.DistrictName, d.Id.ToString()));
            }
        }

        /// <summary>
        /// 刷新网点列表
        /// </summary>
        /// <param name="ownerId">网点id</param>
        protected void RefreshOwnerList(int ownerId)
        {
            this.lstOwners.Items.Clear();
            foreach (Owner o in Utility.GetOwnersByDistrictId(ownerId))
            {
                this.lstOwners.Items.Add(new ListItem(o.OwnerName, o.Id.ToString()));
            }
        }

        /// <summary>
        /// 事件 更改设备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LstDevices_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ShowAddButton(1);
        }

        /// <summary>
        /// 事件更改网点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LstOwners_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ShowAddButton(3);
        }

        /// <summary>
        /// 根据选择的控件 显示新增按钮 新增:区县\网点\设备
        /// </summary>
        /// <param name="type">新增的类别</param>
        protected void ShowAddButton(int type)
        {
            this.btnAddDevice.Visible = false;
            this.btnAddDistrict.Visible = false;
            this.btnAddOwner.Visible = false;

            switch (type)
            {
                case 1:
                    this.lblTips.Text = "新增设备类型";
                    this.btnAddDevice.Visible = true;
                    break;
                case 2:
                    this.lblTips.Text = "新增区县";
                    this.btnAddDistrict.Visible = true;
                    break;
                case 3:
                    this.lblTips.Text = "新增网点";
                    this.btnAddOwner.Visible = true;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 事件 新增设备按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnAddDevice_Click(object sender, EventArgs e)
        {
            if (this.txtAddName.Text.Length > 0)
            {
                this.lblTips.Text = Utility.DeviceAddNew(this.txtAddName.Text) ? "设备新增成功" : "设备新增失败";
            }

            this.RefreshDeviceList();
        }

        /// <summary>
        /// 事件 新增区县按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnAddDistrict_Click(object sender, EventArgs e)
        {
            if (this.txtAddName.Text.Length > 0)
            {
                this.lblTips.Text = Utility.DistrictAddNew(this.txtAddName.Text) ? "区县新增成功" : "区县新增失败";
            }

            this.RefreshDistrictList();
        }

        /// <summary>
        /// 事件 新增网点按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnAddOwner_Click(object sender, EventArgs e)
        {
            if (this.txtAddName.Text.Length > 0)
            {
                this.lblTips.Text =
                    Utility.OwnerAddNew(int.Parse(this.lstDistricts.SelectedValue), this.txtAddName.Text) ? "网点新增成功" : "网点新增失败";
            }

            this.RefreshOwnerList(0);
        }

        /// <summary>
        /// 事件 选择产品信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DataProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txtUpdateSerialNO.Text = this.dataProducts.SelectedDataKey["SerialNO"].ToString();
            this.txtUpdatePsamNO.Text = this.dataProducts.SelectedDataKey["PsamNO"].ToString();
            this.txtUpdateRomVersion.Text = this.dataProducts.SelectedDataKey["RomVersion"].ToString();
        }

        /// <summary>
        /// 事件 更新产品信息按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnUpdateProduct_Click(object sender, EventArgs e)
        {
            Utility.ProductUpdate(
                int.Parse(this.dataProducts.SelectedDataKey["Id"].ToString()),
                this.txtUpdateSerialNO.Text,
                this.txtUpdatePsamNO.Text,
                this.txtUpdateRomVersion.Text);
        }
    }
}